﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using HYoloLabeler.Configs;

namespace HYoloLabeler
{
    public partial class NewBuiltForm : Form
    {
        /// <summary>
        /// 新建的数据集名称
        /// </summary>
        public string? dataSet { get; private set; }
        /// <summary>
        /// 新建的数据集文件夹名称
        /// </summary>
        public string? dataSetFolder { get; private set; }
        /// <summary>
        /// 父窗体数据存放路径
        /// </summary>
        public string? folderPath { get; set; }
        public NewBuiltForm()
        {
            InitializeComponent();
        }

        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txt_DataSet.Text))
            {
                MessageBox.Show("数据集名称不能为空！");
                return;
            }
            else if (this.txt_DataSet.Text.Length > 50)
            {
                MessageBox.Show("数据集名称太长！");
                return;
            }
            var path = "" + folderPath + "\\datasets\\" + txt_DataSet.Text;
            if (System.IO.Directory.Exists(path))
            {
                MessageBox.Show("数据集已存在，请换个目录！");
                return;
            }
            else
            {
                dataSetFolder = this.txt_DataSet.Text;
                Directory.CreateDirectory(path);
                Directory.CreateDirectory(path+ "\\images");
                Directory.CreateDirectory(path + "\\labels");
                // 使用 File.Create 创建文件并立即关闭
                File.Create(Path.Combine(path, "dataset.yaml")).Close();
                var yamlFileManager = new YamlFileManager(path + "\\dataset.yaml");
                YOLOv8Config? yoloConfig = new YOLOv8Config();
                // 修改配置并保存
                yoloConfig.Path = "../datasets/" + txt_DataSet.Text;
                yoloConfig.Train = new List<string> { "images" };
                yoloConfig.Val = new List<string> { "images" };
                yoloConfig.Names = new Dictionary<int, string>{};
                yamlFileManager.SaveYamlConfig(yoloConfig);
            }

            dataSet = path + "\\dataset.yaml";
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
